package com.yanqu.road.server.cmd.activity.springdinner;

import com.yanqu.road.pb.activity.SpringDinnerProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.springdinner.SpringDinnerMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_SPRINGDINNER_RP_TABLE, desc = "请求桌子")
public class SpringDinnerTableCmd extends AbstractCommand {

    private int activityId;
    private boolean isGetGhost;
    SpringDinnerProto.RedPacketTableReqMsg reqMsg;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = SpringDinnerProto.RedPacketTableReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        isGetGhost = reqMsg.getIsGetGhost();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(SpringDinnerMgr.getShowTimeActivityId() == 0 || SpringDinnerMgr.getShowTimeActivityId() != activityId){
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        if(!SpringDinnerMgr.isRedPacketOpen()){
            ret = GameErrorCode.E_SPRINGDINNER_RED_TABLE_NOT_OPEN;
            return;
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            SpringDinnerProto.SpringDinnerComposeRespMsg.Builder respMsg = SpringDinnerProto.SpringDinnerComposeRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(Protocol.U_SPRINGDINNER_RP_ENTER, respMsg);
        }else{
            SpringDinnerProto.CrossTableRedPacketReqMsg.Builder msg = SpringDinnerProto.CrossTableRedPacketReqMsg.newBuilder();
            msg.setActivityId(activityId);
            msg.setIsGetGhost(isGetGhost);
            msg.addAllPage(reqMsg.getPageList());
            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C2_SPRINGDINNER_ACTIVITY_TABLE, msg);
            GamePlayerMgr.sendPacket(player.getUserId(), pbMsg);
        }
    }
}
